import { Controller, Get, Post, Body, Patch, Param, Delete, Res, Header } from '@nestjs/common';
import { BorrowService } from './borrow.service';
import { CreateBorrowDto } from './dto/create-borrow.dto';
import { UpdateBorrowDto } from './dto/update-borrow.dto';
import { Result } from 'src/utils/Result';
import { ApiBody, ApiOperation, ApiParam, ApiQuery, ApiTags } from '@nestjs/swagger';
import { exportToExcel } from 'src/utils';

@ApiTags('借阅记录')
@Controller('borrow')
export class BorrowController {
  constructor(private readonly borrowService: BorrowService) {}

  @ApiOperation({description: '借书'})
  @ApiBody({ type: CreateBorrowDto })
  @Post('add')
  async create(@Body() createCategoryDto: CreateBorrowDto) {
    const result = await this.borrowService.create(createCategoryDto);
    return Result.success(result)
  }
  @ApiOperation({description: '修改借阅状态'})
  @ApiParam({name: 'id', required: true, description: '记录id'})
  @ApiBody({ type: UpdateBorrowDto })
  @Patch('update/:id')
  async update(@Param('id') id: string, @Body() updateCategoryDto: UpdateBorrowDto) {
    const result = await this.borrowService
    .update(+id, updateCategoryDto);
    return Result.success(result)
  }

  @ApiOperation({description: '借阅记录列表'})
  @ApiBody({ schema: {
    default: {pageIndex: 0, pageSize: 10, user_name: ''}
  } })
  @Post('list')
  async findPageList(@Body() data: {pageIndex: number, pageSize: number, card_id: string, user_name: string}) {
    const result = await this.borrowService.findAll(data);
    return Result.success(result)
  }
  @ApiOperation({description: '导出借阅记录列表'})
  @ApiBody({ schema: {
    default: {pageIndex: 0, pageSize: 10, user_name: ''}
  } })
  @Header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
  @Header('Content-Disposition', `attachment; filename="${Date.now()}.xlsx`)
  @Post('export_excel')
  async exportBorrowExcel(@Body() data: {pageIndex: number, pageSize: number, card_id: string, user_name: string}, @Res() res) {
    const { list } = await this.borrowService.findAll(data);
    const headers = [
        { prop: 'id', name: '编号'},
        { prop: 'user_name', name: '借阅者'},
        { prop: 'card_id', name: '借书证号'},
        { prop: 'phone', name: '手机号'},
        { prop: 'book_name', name: '图书名称'},
        { prop: 'category_name', name: '图书类型'},
        { prop: 'status', name: '借阅状态', format: {'0':'再借', '1': '已归还', '2': '逾期'}},
        { prop: 'borrow_date', name: '借阅时间', datetime: true, customFormat: 'YYYY-MM-DD hh:mm:ss'},
        { prop: 'return_date', name: '归还时间', datetime: true, customFormat: 'YYYY-MM-DD hh:mm:ss'},
        { prop: 'actual_return_date', name: '实际归还时间'},
    ]
    await exportToExcel(headers, list, res)
    res.end()
  }
  @ApiOperation({description: '借阅记录详情'})
  @ApiQuery({name: 'id', required: true, description: '记录id'})
  @Get('query/:id')
  async findOne(@Param('id') id: string) {
    const result = await this.borrowService.findOne(+id);
    return Result.success(result)
  }
  @ApiOperation({description: '根据id删除借阅记录'})
  @ApiParam({name: 'id', required: true, description: '记录id'})
  @Delete('del/:id')
  async remove(@Param('id') id: string) {
    const result = await this.borrowService.remove(+id);
    return Result.success(result)
  }
}
